package cluster

import (
	"gomod.alauda.cn/mysql-mgr-operator/pkg/constant"
)

type StartOption struct {
}

type Start struct {
	*StartOption
	*Base
}

func NewStart(opt *StartOption) *Start {
	return &Start{
		StartOption: opt,
	}
}

func (r *Start) WithBase(base *Base) *Start {
	r.Base = base
	return r
}

func (r *Start) Run() error {
	switch r.Type {
	case TypeMysql:
		return r.startMysql()
	case TypeMgr:
		return r.startMgr()
	default:
		err := r.startMysql()
		if err != nil {
			return r.startMgr()
		}
	}
	return nil
}

func (r *Start) startMysql() error {
	cr, err := r.getMysqlCr()
	if err != nil {
		return err
	}
	delete(cr.Annotations, constant.MgrStop)
	err = r.update(cr)
	if err != nil {
		return err
	}
	return nil
}

func (r *Start) startMgr() error {
	cr, err := r.getMgrCr()
	if err != nil {
		return err
	}
	delete(cr.Annotations, constant.MgrStop)
	err = r.update(cr)
	if err != nil {
		return err
	}
	return nil
}
